package m202405.d12;

/**
 * @@author: 爱做梦的锤子
 * @date: 2024/5/12 15:46
 */
public class Q2 {

    public static void main(String[] args) {
        ListNode list1 = new ListNode(2);
        list1.next = new ListNode(4);
        list1.next.next = new ListNode(3);

        ListNode list2 = new ListNode(5);
        list2.next = new ListNode(6);
        list2.next.next = new ListNode(4);
        Q2 q2 = new Q2();
        q2.addTwoNumbers(list1, list2);
    }

    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode rs = new ListNode();
        ListNode p = rs;
        int add = 0;
        while (l1 != null || l2 != null) {
            int sum = add;
            if (l1 != null) {
                sum = sum + l1.val;
            }
            if (l2 != null) {
                sum = sum + l2.val;
            }
            add = sum >= 10 ? sum / 10 : 0;
            p.next = new ListNode(sum % 10);
            p = p.next;
            l1 = l1 == null ? null : l1.next;
            l2 = l2 == null ? null : l2.next;
        }
        if (add > 0) {
            p.next = new ListNode(add);
        }

        return rs.next;
    }
}
